<?php

// Init owncloud
JLoader::register("PJobSeekerModelJobSeeker", OC_App::getAppPath('jobseeker') . "/lib/jobseeker.php");

OCP\JSON::callCheck();
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
}

$sheetid = JRequest::getInt('sheetid', 0);

$user = PFactory::getUser();
$model = new PJobSeekerModelJobSeeker();
$persontestResultInfo = $model->getjobseekertestResultInfo($sheetid);

if(empty($persontestResultInfo)) {
   echo '没有详细信息';
   exit;
}

$persontestResult = $persontestResultInfo->result;

if (!empty($persontestResult)) {
   if (isset($persontestResult['alias']) && isset($persontestResult['result']) && isset($persontestResult['details'])) {
      $p_start_time = $p_end_time = 0;
      if(!isNullDate($persontestResultInfo->start_time))
         $p_start_time = PFactory::getDate($persontestResultInfo->start_time)->toUnix();
      if(!isNullDate($persontestResultInfo->end_time))
         $p_end_time   = PFactory::getDate($persontestResultInfo->end_time)->toUnix();
      $usedtime = $p_end_time - $p_start_time;
      $usedtime = ($p_start_time!=0 && $p_end_time!=0) ? $usedtime : 300; /* need a reasonable time for old data */
      $usedtime = ceil($usedtime/60) . '分钟';
      
      if (empty($persontestResult['result'])) {
         echo "<div class='modal-header'>
                  <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                  <h3>".$persontestResultInfo->sheetTitle."</h3>
               </div>
               <div class='modal-body'>
                  <div class='dialogp_content'>
                     <div class='report_top'><b class='large yahei'>您的性格特征不明显。</b><span class='yahei'>提交时间：".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."&nbsp;&nbsp;&nbsp;&nbsp;用时：".$usedtime."</span></div>
                     <div class='report_detail'></div>
                  </div>
               </div>";
         exit();
      }
      
      $sheetalias = $persontestResult['alias'];
      $result     = $persontestResult['result'];
      $details    = $persontestResult['details'];
      
      switch ($sheetalias) {
         default:
         case 'ENNEAGRAM':
         case 'MBTI':
            $persontestAnswer = $model->getPersontestAnswer($sheetalias, $result);
            echo "<div class='modal-header'>
                     <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                     <h3>".$persontestResultInfo->sheetTitle."</h3>
                  </div>
                  <div class='modal-body'>
                     <div class='dialogp_content'>
                        <div class='report_top'><b class='large yahei'>您的测试结果是：".$persontestAnswer->p_title."</b><span class='yahei'>提交时间：".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."&nbsp;&nbsp;&nbsp;&nbsp;用时：".$usedtime."</span></div>
                        <div class='report_detail'>".$persontestAnswer->text."</div>
                     </div>
                  </div>";
            break;
         case 'HOLLAND':
            $resultText = '';
            $contesntText = '';
            $strengthText = '';
            $patternArr = str_split($result);
            $hAnswers = $model->getPersontestAnswer($sheetalias, $patternArr);
            $aCount = count($hAnswers);
            $find = array('（S）', '（E）', '（C）', '（R）', '（I）', '（A）');
            
            foreach ($hAnswers as $key => $hAnswer) {
               $pAlias = $patternArr[$key];
               $hCount = $details[$pAlias];
               if ($hCount >= 7 && $hCount <= 10) {
                  $strengthText = '强烈的';
               } elseif ($hCount >= 4 && $hCount <= 6) {
                  $strengthText = '较明显的';
               } else {
                  $strengthText = '稍微的';
               }
               
               $resultText .= $strengthText.str_ireplace($find, '', $hAnswer->p_title).'倾向'.($key < ($aCount -1) ? '、' : '');
               $contesntText .= '<b>'.$hAnswer->p_title.'</b><br/>'.$hAnswer->text;
            }
            
            echo "<div class='modal-header'>
                     <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                     <h3>".$persontestResultInfo->sheetTitle."</h3>
                  </div>
                  <div class='modal-body'>
                     <div class='dialogp_content'>
                        <div class='report_top'><b class='large yahei'>您的测试结果是：".$resultText."</b><span class='yahei'>提交时间：".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."&nbsp;&nbsp;&nbsp;&nbsp;用时：".$usedtime."</span></div>
                        <div class='report_detail'>".$contesntText."</div>
                     </div>
                  </div>";
            break;
      }
      exit();
   }
} else {
   echo '系统错误';
   exit();
}

